//
//  SHDZViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "SHDZViewController.h"

#import "SHDZCell.h"
#import "ADDSHDZViewController.h"
#import "UpdateDZViewController.h"
@interface SHDZViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSMutableArray *array;//数据源
    UILabel *labTit;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation SHDZViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"收货地址"];
    array = [NSMutableArray array];
    
    [self loadData];
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 60*array.count-1) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    
    labTit = [[UILabel alloc] init];
    labTit.text = @"您还没有添加地址哦-_-";
    labTit.hidden = YES;
    [self.view addSubview:labTit];
    [labTit makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.centerX);
        make.centerY.equalTo(self.view.centerY);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, ScreenHeight-50, ScreenWidth, 50.0);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"   添加收货地址" forState:UIControlStateNormal];
    [btn setTitleColor:COLOR_RGB(65, 132, 255, 1) forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"矩形-21-拷贝"] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void)click{
    ADDSHDZViewController *adddz = [[ADDSHDZViewController alloc] init];
    [self.navigationController pushViewController:adddz animated:YES];
    
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    SHDZCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"SHDZCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = array[indexPath.row];
    cell.dic = dic;
    
    NSString *province = [dic objectForKey:@"province"];
    NSString *city = [dic objectForKey:@"city"];
    NSString *area = [dic objectForKey:@"area"];
    NSString *detailedadd = [dic objectForKey:@"detailedadd"];
    if ([province isEqualToString:city]) {
        cell.labPlace.text = [NSString stringWithFormat:@"%@%@%@",city,area,detailedadd];
    }else{
        cell.labPlace.text = [NSString stringWithFormat:@"%@%@%@%@",province,city,area,detailedadd];
    }
    
    cell.labPhone.text = [dic objectForKey:@"userphone"];
    NSString *sex = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"sex"] integerValue]];
    
    NSString *name = [NSString stringWithFormat:@"%@",[dic objectForKey:@"consignee"]];
    if ([sex integerValue] == 2) {
        cell.labName.text = [NSString stringWithFormat:@"%@(先生)",name];
    }else{
        cell.labName.text = [NSString stringWithFormat:@"%@(女士)",name];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UpdateDZViewController *dz = [[UpdateDZViewController alloc] init];
    [self.navigationController pushViewController:dz animated:YES];
    NSDictionary *dic = array[indexPath.row];
    NSString *addressid = [NSString stringWithFormat:@"%li",[[dic objectForKey:@"id"] integerValue]];
    dz.addressid = addressid;
    dz.dic = dic;
    
    //
    /*
    [self loadData2:addressid];
    */
}
//显示收货地址
- (void)loadData{
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/listDeliveryAddress",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"收货地址res:%@",responseObject);
        NSString *status = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        if ([status integerValue] == 200) {
            array = [responseObject objectForKey:@"content"];
        }else{
            _tableView.hidden = YES;
            labTit.hidden = NO;
        }
        
        [self loadUI];
        [_tableView reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}
//设置默认收货地址
- (void)loadData2:(NSString *)addressid{
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:addressid forKey:@"addressid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/settingdefaultadd",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"设置默认收货地址res:%@",responseObject);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
